home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / clx.lha / clx / text.l < prev    next >
Lisp/Scheme  |  1988-09-12  |  45KB  |  1,119 lines

  1. ;;; -*- Mode: LISP; Syntax: Common-lisp; Package: XLIB; Base: 10; Lowercase: Yes -*-
  2.  
  3. ;;; CLX text keyboard and pointer requests
  4.  
  5. ;;;
  6. ;;;             TEXAS INSTRUMENTS INCORPORATED
  7. ;;;                  P.O. BOX 2909
  8. ;;;                   AUSTIN, TEXAS 78769
  9. ;;;
  10. ;;; Copyright (C) 1987 Texas Instruments Incorporated.
  11. ;;;
  12. ;;; Permission is granted to any individual or institution to use, copy, modify,
  13. ;;; and distribute this software, provided that this complete copyright and
  14. ;;; permission notice is maintained, intact, in all copies and supporting
  15. ;;; documentation.
  16. ;;;
  17. ;;; Texas Instruments Incorporated provides this software "as is" without
  18. ;;; express or implied warranty.
  19. ;;;
  20.  
  21. (in-package 'xlib :use '(lisp))
  22.  
  23. (export '(
  24.       translation-function
  25.       translate-default
  26.       text-extents
  27.       text-width
  28.       draw-glyph
  29.       draw-glyphs
  30.       draw-image-glyph
  31.       draw-image-glyphs
  32.       display-keycode-range
  33.       set-modifier-mapping
  34.       modifier-mapping
  35.       keysym
  36.       change-keyboard-mapping
  37.       keyboard-mapping
  38.       keyboard-mapping
  39.       ))
  40.  
  41. ;; Strings are broken up into chunks of this size
  42. (defparameter *max-string-size* 254)
  43.  
  44. ;; In the functions below, the transform is used to convert an element of the
  45. ;; sequence into a font index.  The transform is applied to each element of the
  46. ;; (sub)sequence, until either the transform returns nil or the end of the
  47. ;; (sub)sequence is reached.  If transform returns nil for an element, the
  48. ;; index of that element in the sequence is returned, otherwise nil is
  49. ;; returned.
  50.  
  51. (deftype translation-function ()
  52.   '(function (sequence array-index array-index (or null font) vector array-index)
  53.          (values array-index (or null int16 font) (or null int32))))
  54.  
  55. ;; In the functions below, if width is specified, it is assumed to be the pixel
  56. ;; width of whatever string of glyphs is actually drawn.  Specifying width will
  57. ;; allow for appending the output of subsequent calls to the same protocol
  58. ;; request, provided gcontext has not been modified in the interim.  If width
  59. ;; is not specified, appending of subsequent output might not occur.
  60. ;; Specifying width is simply a hint, for performance.  Note that specifying
  61. ;; width may be difficult if transform can return nil.
  62.  
  63. (defun translate-default (src src-start src-end font dst dst-start)
  64.   ;; dst is guaranteed to have room for (- src-end src-start) integer elements,
  65.   ;; starting at dst-start; whether dst holds 8-bit or 16-bit elements depends
  66.   ;; on context.  font is the current font, if known.  The function should
  67.   ;; translate as many elements of src as possible into indexes in the current
  68.   ;; font, and store them into dst.
  69.   ;;
  70.   ;; The first return value should be the src index of the first untranslated
  71.   ;; element.  If no further elements need to be translated, the second return
  72.   ;; value should be nil.  If a horizontal motion is required before further
  73.   ;; translation, the second return value should be the delta in x coordinate.
  74.   ;; If a font change is required for further translation, the second return
  75.   ;; value should be the new font.  If known, the pixel width of the translated
  76.   ;; text can be returned as the third value; this can allow for appending of
  77.   ;; subsequent output to the same protocol request, if no overall width has
  78.   ;; been specified at the higher level.
  79.   ;; (returns values: ending-index
  80.   ;;                  (OR null horizontal-motion font)
  81.   ;;                  (OR null translated-width))
  82.   (declare (type sequence src)
  83.        (type array-index src-start src-end dst-start)
  84.        (type (or null font) font)
  85.        (type vector dst)
  86.        (inline graphic-char-p))
  87.   (declare-values integer (or null integer font) (or null integer))
  88.   font ;;not used
  89.   (if (stringp src)
  90.       (do ((i src-start (index+ i 1))
  91.        (j dst-start (index+ j 1))
  92.        (char))
  93.       ((index>= i src-end)
  94.        i)
  95.     (declare (type array-index i j))
  96.     (if (graphic-char-p (setq char (char src i)))
  97.         (setf (aref dst j) (char->card8 char))
  98.       (return i)))
  99.       (do ((i src-start (index+ i 1))
  100.        (j dst-start (index+ j 1))
  101.        (elt))
  102.       ((index>= i src-end)
  103.        i)
  104.     (declare (type array-index i j))
  105.     (setq elt (elt src i))
  106.     (cond ((and (characterp elt) (graphic-char-p elt))
  107.            (setf (aref dst j) (char->card8 elt)))
  108.           ((integerp elt)
  109.            (setf (aref dst j) elt))
  110.           (t
  111.            (return i))))))
  112.  
  113. ;; There is a question below of whether translate should always be required, or
  114. ;; if not, what the default should be or where it should come from.  For
  115. ;; example, the default could be something that expected a string as src and
  116. ;; translated the CL standard character set to ASCII indexes, and ignored fonts
  117. ;; and bits.  Or the default could expect a string but otherwise be "system
  118. ;; dependent".  Or the default could be something that expected a vector of
  119. ;; integers and did no translation.  Or the default could come from the
  120. ;; gcontext (but what about text-extents and text-width?).
  121.  
  122. (defun text-extents (font sequence &key (start 0) end translate)
  123.   ;; If multiple fonts are involved, font-ascent and font-descent will be the
  124.   ;; maximums.  If multiple directions are involved, the direction will be nil.
  125.   ;; Translate will always be called with a 16-bit dst buffer.
  126.   (declare (type sequence sequence)
  127.        (type (or null font gcontext) font)
  128.        (type (or null translation-function) translate)) ;; Defaults to #'translate-default
  129.   (declare-values width ascent descent left right
  130.           font-ascent font-descent direction
  131.           (or null array-index))
  132.   (when (type? font 'gcontext)
  133.     (force-gcontext-changes font)
  134.     (setq font (gcontext-font font t)))
  135.   (let* ((left-bearing 0)
  136.      (right-bearing 0)
  137.      ;; Sum of widths
  138.      (width 0)
  139.      (ascent 0)
  140.      (descent 0)
  141.      (overall-ascent 0)
  142.      (overall-descent 0)
  143.      (overall-direction :unknown)
  144.      (next-start nil)
  145.      (display (font-display font)))
  146.     (declare (type int16 ascent descent overall-ascent overall-descent)
  147.          (type int32 left-bearing right-bearing width)
  148.          (type (or null array-index) next-start)
  149.          (type display display))
  150.     (with-display (display)
  151.       (do* ((wbuf (display-tbuf16 display))
  152.         (src-end (or end (length sequence)))
  153.         (src-start start end)
  154.         (end (index-min src-end (index+ src-start *buffer-text16-size*))
  155.          (index-min src-end (index+ src-start *buffer-text16-size*)))
  156.         (buf-end 0)
  157.         (new-font)
  158.         (font-ascent 0)
  159.         (font-descent 0)
  160.         (font-direction)
  161.         (stop-p nil))
  162.        ((or stop-p (index>= src-start src-end))
  163.         (when (index< src-start src-end)
  164.           (setq next-start src-start)))
  165.     (declare (type buffer-text16 wbuf)
  166.          (type array-index src-start src-end end buf-end)
  167.          (type int16 font-ascent font-descent)
  168.          (type boolean stop-p))
  169.     ;; Translate the text
  170.     (multiple-value-setq (buf-end new-font)
  171.       (funcall (or translate #'translate-default)
  172.            sequence src-start end font wbuf 0))
  173.     (setq buf-end (- buf-end src-start))
  174.     (cond ((null new-font) (setq stop-p t))
  175.           ((integerp new-font) (incf width (the int32 new-font)))
  176.           ((type? new-font 'font) (setq font new-font)))
  177.     
  178.     (let (w a d l r)
  179.       (if (or (font-char-infos-internal font) (font-local-only-p font))
  180.           ;; Calculate text extents locally
  181.           (progn
  182.         (multiple-value-setq (w a d l r)
  183.           (text-extents-local font wbuf 0 buf-end nil))
  184.         (setq font-ascent (the int16 (font-ascent font))
  185.               font-descent (the int16 (font-descent font))
  186.               font-direction (font-direction font)))
  187.         ;; Let the server calculate text extents
  188.         (multiple-value-setq
  189.           (w a d l r font-ascent font-descent font-direction)
  190.           (text-extents-server font wbuf 0 buf-end)))
  191.       (incf width (the int32 w))
  192.       (cond ((index= src-start start)
  193.          (setq left-bearing (the int32 l))
  194.          (setq right-bearing (the int32 r))
  195.          (setq ascent (the int16 a))
  196.          (setq descent (the int16 d)))
  197.         (t
  198.          (setq left-bearing (the int32 (min left-bearing (the int32 l))))
  199.          (setq right-bearing (the int32 (max right-bearing (the int32 r))))
  200.          (setq ascent (the int16 (max ascent (the int16 a))))
  201.          (setq descent (the int16 (max descent (the int16 d)))))))
  202.     
  203.     (setq overall-ascent (the int16 (max overall-ascent font-ascent)))
  204.     (setq overall-descent (the int16 (max overall-descent font-descent)))
  205.     (case overall-direction
  206.       (:unknown (setq overall-direction font-direction))
  207.       (:left-to-right (unless (eq font-direction :left-to-right)
  208.                 (setq overall-direction nil)))
  209.       (:right-to-left (unless (eq font-direction :right-to-left)
  210.                 (setq overall-direction nil))))))
  211.     
  212.     (values width
  213.         ascent
  214.         descent
  215.         left-bearing
  216.         right-bearing
  217.         overall-ascent
  218.         overall-descent
  219.         overall-direction
  220.         next-start)))
  221.  
  222. (defun text-width (font sequence &key (start 0) end translate)
  223.   ;; Translate will always be called with a 16-bit dst buffer.
  224.   (declare (type sequence sequence)
  225.        (type (or null font gcontext) font)
  226.        (type array-index start)
  227.        (type (or null array-index) end)
  228.        (type (or null translation-function) translate)) ;; Defaults to #'translate-default
  229.   (declare-values integer (or null integer))
  230.   (when (type? font 'gcontext)
  231.     (force-gcontext-changes font)
  232.     (setq font (gcontext-font font t)))
  233.   (let* ((width 0)
  234.      (next-start nil)
  235.      (display (font-display font)))
  236.     (declare (type int32 width)
  237.          (type (or null array-index) next-start)
  238.          (type display display))
  239.     (with-display (display)
  240.       (do* ((wbuf (display-tbuf16 display))
  241.         (src-end (or end (length sequence)))
  242.         (src-start start end)
  243.         (end (index-min src-end (index+ src-start *buffer-text16-size*))
  244.          (index-min src-end (index+ src-start *buffer-text16-size*)))
  245.         (buf-end 0)
  246.         (new-font)
  247.         (stop-p nil))
  248.        ((or stop-p (index>= src-start src-end))
  249.         (when (index< src-start src-end)
  250.           (setq next-start src-start)))
  251.     (declare (type buffer-text16 wbuf)
  252.          (type array-index src-start src-end end buf-end)
  253.          (type boolean stop-p))
  254.     ;; Translate the text
  255.     (multiple-value-setq (buf-end new-font)
  256.       (funcall (or translate #'translate-default)
  257.            sequence src-start end font wbuf 0))
  258.     (setq buf-end (- buf-end src-start))
  259.     (cond ((null new-font) (setq stop-p t))
  260.           ((integerp new-font) (incf width (the int32 new-font)))
  261.           ((type? new-font 'font) (setq font new-font)))
  262.     
  263.     (incf width
  264.           (if (or (font-char-infos-internal font) (font-local-only-p font))
  265.           (text-extents-local font wbuf 0 buf-end :width-only)
  266.         (text-width-server font wbuf 0 buf-end)))))
  267.     (values width next-start)))
  268.  
  269. #+clx-little-endian
  270. (defun byte-swap-card16 (card16)
  271.   (declare (type card16 card16))
  272.   (declare-values card16)
  273.   (dpb card16 (byte 8 8) (ash card16 -8)))  
  274.  
  275. (defun text-extents-server (font string start end)
  276.   (declare (type font font)
  277.        (type string string)
  278.        (type array-index start end))
  279.   (declare-values width ascent descent left right font-ascent
  280.           font-descent direction)
  281.   (let ((display (font-display font))
  282.     (length (index- end start))
  283.     width ascent descent left right font-ascent font-descent direction
  284.     (font-id (font-id font)))
  285.     (declare (type display display)
  286.          (type array-index length)
  287.          (type resource-id font-id))
  288.     (with-display (display)
  289.       (with-buffer-request (display *x-querytextextents* :no-after)
  290.     ((data boolean) (oddp length))
  291.     (length (index+ (index-ceiling length 2) 2))
  292.     (resource-id font-id)
  293.     ((sequence :format card16 :start start :end end :appending t
  294.            #+clx-little-endian :transform
  295.            #+clx-little-endian ;; Byte swap for little-endian
  296.            #'byte-swap-card16)
  297.      string))
  298.       (with-buffer-reply (display 28 :sizes (8 16 32))
  299.     (setq direction (member8-get 1 :left-to-right :right-to-left)
  300.           font-ascent (int16-get 8)
  301.           font-descent (int16-get 10)
  302.           ascent (int16-get 12)
  303.           descent (int16-get 14)
  304.           width (integer-get 16)
  305.           left (integer-get 20)
  306.           right (integer-get 24))))
  307.     (display-invoke-after-function display)
  308.     (values width ascent descent left right font-ascent font-descent direction)))
  309.  
  310. (defun text-width-server (font string start end)
  311.   (declare (type (or font gcontext) font)
  312.        (type string string)
  313.        (type array-index start end))
  314.   (declare-values integer)
  315.   (let ((display (font-display font))
  316.     (length (index- end start))
  317.     width
  318.     (font-id (font-id font)))
  319.     (declare (type display display)
  320.          (type array-index length)
  321.          (type resource-id font-id))
  322.     (with-display (display)
  323.       (with-buffer-request (display *x-querytextextents* :no-after)
  324.     ((data boolean) (oddp length))
  325.     (length (index+ (index-ceiling length 2) 2))
  326.     (resource-id font-id)
  327.     ((sequence :format card16 :start start :end end :appending t
  328.            #+clx-little-endian :transform
  329.            #+clx-little-endian ;; Byte swap for little-endian
  330.            #'byte-swap-card16)
  331.      string))
  332.       (with-buffer-reply (display 28 :sizes 32)
  333.     (setq width (integer-get 16))))
  334.     (display-invoke-after-function display)
  335.     width))
  336.  
  337. (defun text-extents-local (font sequence start end width-only-p)
  338.   (declare (type font font)
  339.        (type sequence sequence)
  340.        (type integer start end)
  341.        (type boolean width-only-p))
  342.   (declare-values width ascent descent overall-left overall-right)
  343.   (let* ((char-infos (font-char-infos font))
  344.      (font-info (font-font-info font)))
  345.     (declare (type font-info font-info))
  346.     (declare-array vector char-infos)
  347.     (if (zerop (length char-infos))
  348.     ;; Fixed width font
  349.     (let* ((font-width (max-char-width font))
  350.            (font-ascent (max-char-ascent font))
  351.            (font-descent (max-char-descent font))
  352.            (width (* (index- end start) font-width)))
  353.       (declare (type int16 font-width font-ascent font-descent)
  354.            (type int32 width))
  355.       (if width-only-p
  356.           width
  357.         (values width
  358.             font-ascent
  359.             font-descent
  360.             (max-char-left-bearing font)
  361.             (+ width (- font-width) (max-char-right-bearing font)))))
  362.       
  363.       ;; Variable-width font
  364.       (let* ((first-col (font-info-min-byte2 font-info))
  365.          (num-cols (1+ (- (font-info-max-byte2 font-info) first-col)))
  366.          (first-row (font-info-min-byte1 font-info))
  367.          (last-row (font-info-max-byte1 font-info))
  368.          (num-rows (1+ (- last-row first-row))))
  369.     (declare (type card8 first-col first-row last-row)
  370.          (type card16 num-cols num-rows))
  371.     (if (or (plusp first-row) (plusp last-row))
  372.         
  373.         ;; Matrix (16 bit) font
  374.         (macrolet ((char-info-elt (sequence elt)
  375.              `(let* ((char (the card16 (elt ,sequence ,elt)))
  376.                  (row (- (ash char -8) first-row))
  377.                  (col (- (logand char #xff) first-col)))
  378.                 (declare (type card16 char)
  379.                      (type int16 row col))
  380.                 (if (and (< -1 row num-rows) (< -1 col num-cols))
  381.                 (index* 6 (index+ (index* row num-cols) col))
  382.                   -1))))
  383.           (if width-only-p
  384.           (do ((i start (index1+ i))
  385.                (width 0))
  386.               ((index>= i end) width)
  387.             (declare (type array-index i)
  388.                  (type int32 width))
  389.             (let ((n (char-info-elt sequence i)))
  390.               (declare (type fixnum n))
  391.               (unless (minusp n)  ;; Ignore characters not in the font
  392.             (incf width (the int16 (aref char-infos (index+ 2 n)))))))
  393.         ;; extents
  394.         (do ((i start (index1+ i))
  395.              (width 0)
  396.              (ascent 0)
  397.              (descent 0)
  398.              (left #x7fff)
  399.              (right 0))
  400.             ((index>= i end)
  401.              (values width ascent descent left right))
  402.           (declare (type array-index i)
  403.                (type int16 ascent descent)
  404.                (type int32 width left right))
  405.           (let ((n (char-info-elt sequence i)))
  406.             (declare (type fixnum n))
  407.             (unless (minusp n) ;; Ignore characters not in the font
  408.               (setq left (min left (+ width (aref char-infos n))))
  409.               (setq right (max right (+ width (aref char-infos (index1+ n)))))
  410.               (incf width (aref char-infos (index+ 2 n)))
  411.               (setq ascent (max ascent (aref char-infos (index+ 3 n))))
  412.               (setq descent (max descent (aref char-infos (index+ 4 n)))))))))
  413.       
  414.       ;; Non-matrix (8 bit) font
  415.       ;; The code here is identical to the above, except for the following macro:
  416.       (macrolet ((char-info-elt (sequence elt)
  417.                `(let ((col (- (the card16 (elt ,sequence ,elt)) first-col)))
  418.               (declare (type int16 col))
  419.               (if (< -1 col num-cols)
  420.                   (index* 6 col)
  421.                 -1))))
  422.         (if width-only-p
  423.         (do ((i start (index1+ i))
  424.              (width 0))
  425.             ((index>= i end) width)
  426.           (declare (type array-index i)
  427.                (type int32 width))
  428.           (let ((n (char-info-elt sequence i)))
  429.             (declare (type fixnum n))
  430.             (unless (minusp n) ;; Ignore characters not in the font
  431.               (incf width (the int16 (aref char-infos (index+ 2 n)))))))
  432.           ;; extents
  433.           (do ((i start (index1+ i))
  434.            (width 0)
  435.            (ascent 0)
  436.            (descent 0)
  437.            (left #x7fff)
  438.            (right 0))
  439.           ((index>= i end)
  440.            (values width ascent descent left right))
  441.         (declare (type array-index i)
  442.              (type int16 ascent descent)
  443.              (type int32 width left right))
  444.         (let ((n (char-info-elt sequence i)))
  445.           (declare (type fixnum n))
  446.           (unless (minusp n) ;; Ignore characters not in the font
  447.             (setq left (min left (+ width (aref char-infos n))))
  448.             (setq right (max right (+ width (aref char-infos (index1+ n)))))
  449.             (incf width (aref char-infos (index+ 2 n)))
  450.             (setq ascent (max ascent (aref char-infos (index+ 3 n))))
  451.             (setq descent (max descent (aref char-infos (index+ 4 n)))))
  452.           ))))
  453.       )))))
  454.  
  455. ;;-----------------------------------------------------------------------------
  456.  
  457. ;; This controls the element size of the dst buffer given to translate.  If
  458. ;; :default is specified, the size will be based on the current font, if known,
  459. ;; and otherwise 16 will be used.  [An alternative would be to pass the buffer
  460. ;; size to translate, and allow it to return the desired size if it doesn't
  461. ;; like the current size.  The problem is that the protocol doesn't allow
  462. ;; switching within a single request, so to allow switching would require
  463. ;; knowing the width of text, which isn't necessarily known.  We could call
  464. ;; text-width to compute it, but perhaps that is doing too many favors?]  [An
  465. ;; additional possibility is to allow an index-size of :two-byte, in which case
  466. ;; translate would be given a double-length 8-bit array, and translate would be
  467. ;; expected to store first-byte/second-byte instead of 16-bit integers.]
  468.  
  469. (deftype index-size () '(member :default 8 16))
  470.  
  471. ;; In the functions below, if width is specified, it is assumed to be the total
  472. ;; pixel width of whatever string of glyphs is actually drawn.  Specifying
  473. ;; width will allow for appending the output of subsequent calls to the same
  474. ;; protocol request, provided gcontext has not been modified in the interim.
  475. ;; If width is not specified, appending of subsequent output might not occur
  476. ;; (unless translate returns the width).  Specifying width is simply a hint,
  477. ;; for performance.
  478.  
  479. (defun draw-glyph (drawable gcontext x y elt
  480.            &key translate width (size :default))
  481.   ;; Returns true if elt is output, nil if translate refuses to output it.
  482.   ;; Second result is width, if known.
  483.   (declare (type drawable drawable)
  484.        (type gcontext gcontext)
  485.        (type int16 x y)
  486.        (type (or null translation-function) translate) ;; Defaults to #'translate-default
  487.        (type (or null int32) width)
  488.        (type index-size size))
  489.   (declare-values boolean (or null int32))
  490.   (let* ((display (gcontext-display gcontext))
  491.      (result t)
  492.      (opcode *x-polytext8*))
  493.     (declare (type display display))
  494.     (let ((vector (allocate-gcontext-state)))
  495.       (declare (type gcontext-state vector))
  496.       (setf (aref vector 0) elt)
  497.       (multiple-value-bind (new-start new-font translate-width)
  498.       (funcall (or translate #'translate-default)
  499.            vector 0 1 (gcontext-font gcontext t) vector 1)
  500.     ;; Allow translate to set a new font
  501.     (when (type? new-font 'font) 
  502.       (setf (gcontext-font gcontext) new-font)
  503.       (multiple-value-setq (new-start new-font translate-width)
  504.         (funcall translate vector 0 1 new-font vector 1)))
  505.     ;; If new-start is zero, translate refuses to output it
  506.     (setq result (index-plusp new-start)
  507.           elt (aref vector 1))
  508.     (deallocate-gcontext-state vector)
  509.     (when translate-width (setq width translate-width))))
  510.     (when result
  511.       (when (eql size 16)
  512.     (setq opcode *x-polytext16*)
  513.     (setq elt (dpb elt (byte 8 8) (ldb (byte 8 8) elt))))
  514.       (with-buffer-request (display opcode :gc-force gcontext)
  515.     (drawable drawable)
  516.     (gcontext gcontext)
  517.     (int16 x y)
  518.     (card8 1 0)
  519.     (card8 (ldb (byte 8 0) elt))
  520.     (card8 (ldb (byte 8 8) elt)))
  521.       (values t width))))
  522.   
  523. (defun draw-glyphs (drawable gcontext x y sequence
  524.             &key (start 0) end translate width (size :default))
  525.   ;; First result is new start, if end was not reached.  Second result is
  526.   ;; overall width, if known.
  527.   (declare (type drawable drawable)
  528.        (type gcontext gcontext)
  529.        (type int16 x y)
  530.        (type array-index start)
  531.        (type sequence sequence)
  532.        (type (or null array-index) end)
  533.        (type (or null translation-function) translate) 
  534.        (type (or null int32) width)
  535.        (type index-size size))
  536.   (declare-values (or null array-index) (or null int32))
  537.   (unless end (setq end (length sequence)))
  538.   (ecase size
  539.     ((:default 8) (draw-glyphs8 drawable gcontext x y sequence start end
  540.                 (or translate #'translate-default) width))
  541.     (16 (draw-glyphs16 drawable gcontext x y sequence start end
  542.                (or translate #'translate-default) width))))
  543.  
  544. (defun draw-glyphs8 (drawable gcontext x y sequence start end translate width)
  545.   ;; First result is new start, if end was not reached.  Second result is
  546.   ;; overall width, if known.
  547.   (declare (type drawable drawable)
  548.        (type gcontext gcontext)
  549.        (type int32 x y width)
  550.        (type array-index start end)
  551.        (type sequence sequence))
  552.   (declare-values (or null array-index) (or null int32))
  553.   (declare-funarg translation-function translate) 
  554.   (let* ((src-start start)
  555.      (src-end (or end (length sequence)))
  556.      (next-start nil)
  557.      (length (index- src-end src-start))
  558.      (display (gcontext-display gcontext))
  559.      ;; Should metrics-p be T?  Don't want to pass a NIL font into translate...
  560.      (font (gcontext-font gcontext t)))
  561.     (declare (type array-index src-start src-end length)
  562.          (type (or null array-index) next-start)
  563.          (type display display))
  564.     (with-buffer-request (display *x-polytext8* :gc-force gcontext :length length)
  565.       (drawable drawable)
  566.       (gcontext gcontext)
  567.       (int16 x y)
  568.       (progn
  569.     (do* ((boffset (index+ buffer-boffset 16))
  570.           (src-chunk 0)
  571.           (dst-chunk 0)
  572.           (offset 0)
  573.           (overall-width 0)
  574.           (stop-p nil))
  575.          ((or stop-p (zerop length))
  576.           ;; Ensure terminated with zero bytes
  577.           (do ((end (the array-index (lround boffset))))
  578.           ((index>= boffset end))
  579.         (setf (aref buffer-bbuf boffset) 0)
  580.         (index-incf boffset))
  581.           (length-put 2 (index-ash (index- boffset buffer-boffset) -2))
  582.           (setf (buffer-boffset display) boffset)
  583.           (unless (index-zerop length) (setq next-start src-start))
  584.           (when overall-width (setq width overall-width)))
  585.  
  586.       (declare (type array-index src-chunk dst-chunk offset)
  587.            (type (or null int32) overall-width)
  588.            (type boolean stop-p))
  589.       (setq src-chunk (index-min length *max-string-size*))
  590.       (multiple-value-bind (new-start new-font translated-width)
  591.           (funcall translate
  592.                sequence src-start (index+ src-start src-chunk)
  593.                font buffer-bbuf (index+ boffset 2))
  594.         (setq dst-chunk (index- new-start src-start)
  595.           length (index- length dst-chunk)
  596.           src-start new-start)
  597.         (if translated-width
  598.         (when overall-width (incf overall-width translated-width))
  599.           (setq overall-width nil))
  600.         (when (index-plusp dst-chunk)
  601.           (setf (aref buffer-bbuf boffset) dst-chunk)
  602.           (setf (aref buffer-bbuf (index+ boffset 1)) offset)
  603.           (incf boffset (index+ dst-chunk 2)))
  604.         (setq offset 0)
  605.         (cond ((null new-font)
  606.            ;; Don't stop if translate copied whole chunk
  607.            (unless (index= src-chunk dst-chunk)
  608.              (setq stop-p t)))
  609.           ((integerp new-font) (setq offset new-font))
  610.           ((type? new-font 'font)
  611.            (setq font new-font)
  612.            (let ((font-id (font-id font))
  613.              (buffer-boffset boffset))
  614.              (declare (type resource-id font-id)
  615.                   (type array-index buffer-boffset))
  616.              ;; This changes the gcontext font in the server
  617.              ;; Update the gcontext cache (both local and server state)
  618.              (setf (gcontext-internal-font-obj (gcontext-local-state gcontext)) font
  619.                (gcontext-internal-font-obj (gcontext-server-state gcontext)) font
  620.                (gcontext-internal-font (gcontext-local-state gcontext)) font-id
  621.                (gcontext-internal-font (gcontext-server-state gcontext)) font-id)
  622.              (card8-put 0 #xff)
  623.              (card8-put 1 (ldb (byte 8 24) font-id))
  624.              (card8-put 2 (ldb (byte 8 16) font-id))
  625.              (card8-put 3 (ldb (byte 8 8) font-id))
  626.              (card8-put 4 (ldb (byte 8 0) font-id)))
  627.            (index-incf boffset 5)))
  628.         ))))
  629.     (values next-start width)))
  630.  
  631. ;; NOTE: After the first font change by the TRANSLATE function, characters are no-longer
  632. ;;       on 16bit boundaries and this function garbles the bytes.
  633. (defun draw-glyphs16 (drawable gcontext x y sequence start end translate width)
  634.   ;; First result is new start, if end was not reached.  Second result is
  635.   ;; overall width, if known.
  636.   (declare (type drawable drawable)
  637.        (type gcontext gcontext)
  638.        (type int16 x y)
  639.        (type array-index start end)
  640.        (type int32 width)
  641.        (type sequence sequence))
  642.   (declare-values (or null array-index) (or null int32))
  643.   (declare-funarg translation-function translate)
  644.   (let* ((src-start start)
  645.      (src-end (or end (length sequence)))
  646.      (next-start nil)
  647.      (length (- src-end src-start))
  648.      (display (gcontext-display gcontext))
  649.      ;; Should metrics-p be T?  Don't want to pass a NIL font into translate...
  650.      (font (gcontext-font gcontext t))
  651.      (buffer (display-tbuf16 display)))
  652.     (declare (type array-index src-start src-end length)
  653.          (type (or null array-index) next-start)
  654.          (type display display)
  655.          (type buffer-text16 buffer))
  656.     (with-buffer-request (display *x-polytext16* :gc-force gcontext :length length)
  657.       (drawable drawable)
  658.       (gcontext gcontext)
  659.       (int16 x y)
  660.       (progn
  661.     (do* ((boffset (index+ buffer-boffset 16))
  662.           (src-chunk 0)
  663.           (dst-chunk 0)
  664.           (offset 0)
  665.           (overall-width 0)
  666.           (stop-p nil))
  667.          ((or stop-p (zerop length))
  668.           ;; Ensure terminated with zero bytes
  669.           (do ((end (lround boffset)))
  670.           ((index>= boffset end))
  671.         (setf (aref buffer-bbuf boffset) 0)
  672.         (index-incf boffset))
  673.           (length-put 2 (index-ash (index- boffset buffer-boffset) -2))
  674.           (setf (buffer-boffset display) boffset)
  675.           (unless (zerop length) (setq next-start src-start))
  676.           (when overall-width (setq width overall-width)))
  677.  
  678.       (declare (type array-index boffset src-chunk dst-chunk offset)
  679.            (type (or null int32) overall-width)
  680.            (type boolean stop-p))
  681.       (setq src-chunk (index-min length *max-string-size*))
  682.       (multiple-value-bind (new-start new-font translated-width)
  683.           (funcall translate
  684.                sequence src-start (index+ src-start src-chunk)
  685.                font buffer 0)
  686.         (setq dst-chunk (index- new-start src-start)
  687.           length (index- length dst-chunk)
  688.           src-start new-start)
  689.         (write-sequence-card16 display (index+ boffset 2) buffer 0 dst-chunk
  690.                    #+clx-little-endian ;; Byte swap for little-endian
  691.                    #'byte-swap-card16)
  692.         (if translated-width
  693.         (when overall-width (incf overall-width translated-width))
  694.           (setq overall-width nil))
  695.         (when (index-plusp dst-chunk)
  696.           (setf (aref buffer-bbuf boffset) dst-chunk)
  697.           (setf (aref buffer-bbuf (index+ boffset 1)) offset)
  698.           (index-incf boffset (index+ dst-chunk dst-chunk 2)))
  699.         (setq offset 0)
  700.         (cond ((null new-font)
  701.            ;; Don't stop if translate copied whole chunk
  702.            (unless (index= src-chunk dst-chunk) 
  703.              (setq stop-p t)))
  704.           ((integerp new-font) (setq offset new-font))
  705.           ((type? new-font 'font)
  706.            (setq font new-font)
  707.            (let ((font-id (font-id font))
  708.              (buffer-boffset boffset))
  709.              (declare (type resource-id font-id)
  710.                   (type array-index buffer-boffset))
  711.              ;; This changes the gcontext font in the SERVER
  712.              ;; Update the gcontext cache (both local and server state)
  713.              (setf (gcontext-internal-font-obj (gcontext-local-state gcontext)) font
  714.                (gcontext-internal-font-obj (gcontext-server-state gcontext)) font
  715.                (gcontext-internal-font (gcontext-local-state gcontext)) font-id
  716.                (gcontext-internal-font (gcontext-server-state gcontext)) font-id)
  717.              (card8-put 0 #xff)
  718.              (card8-put 1 (ldb (byte 8 24) font-id))
  719.              (card8-put 2 (ldb (byte 8 16) font-id))
  720.              (card8-put 3 (ldb (byte 8 8) font-id))
  721.              (card8-put 4 (ldb (byte 8 0) font-id)))
  722.            (index-incf boffset 5)))
  723.         ))))
  724.     (values next-start width)))
  725.  
  726. (defun draw-image-glyph (drawable gcontext x y elt
  727.              &key translate width (size :default))
  728.   ;; Returns true if elt is output, nil if translate refuses to output it.
  729.   ;; Second result is overall width, if known.  An initial font change is
  730.   ;; allowed from translate.
  731.   (declare (type drawable drawable)
  732.        (type gcontext gcontext)
  733.        (type int16 x y)
  734.        (type (or null translation-function) translate) ;; Defaults to #'translate-default
  735.        (type (or null int32) width)
  736.        (type index-size size))
  737.   (declare-values boolean (or null int32))
  738.   (let* ((display (gcontext-display gcontext))
  739.      (result t)
  740.      (opcode *x-imagetext8*))
  741.     (declare (type display display))
  742.     (let ((vector (allocate-gcontext-state)))
  743.       (declare (type gcontext-state vector))
  744.       (setf (aref vector 0) elt)
  745.       (multiple-value-bind (new-start new-font translate-width)
  746.       (funcall (or translate #'translate-default)
  747.            vector 0 1 (gcontext-font gcontext t) vector 1)
  748.     ;; Allow translate to set a new font
  749.     (when (type? new-font 'font) 
  750.       (setf (gcontext-font gcontext) new-font)
  751.       (multiple-value-setq (new-start new-font translate-width)
  752.         (funcall translate vector 0 1 new-font vector 1)))
  753.     ;; If new-start is zero, translate refuses to output it
  754.     (setq result (index-plusp new-start)
  755.           elt (aref vector 1))
  756.     (deallocate-gcontext-state vector)
  757.     (when translate-width (setq width translate-width))))
  758.     (when result
  759.       (when (eql size 16)
  760.     (setq opcode *x-imagetext16*)
  761.     (setq elt (dpb elt (byte 8 8) (ldb (byte 8 8) elt))))
  762.       (with-buffer-request (display opcode :gc-force gcontext)
  763.     (drawable drawable)
  764.     (gcontext gcontext)
  765.     (data 1) ;; 1 character
  766.     (int16 x y)
  767.     (card8 (ldb (byte 8 0) elt))
  768.     (card8 (ldb (byte 8 8) elt)))
  769.       (values t width))))
  770.  
  771. (defun draw-image-glyphs (drawable gcontext x y sequence
  772.               &key (start 0) end width translate (size :default))
  773.   ;; An initial font change is allowed from translate, but any subsequent font
  774.   ;; change or horizontal motion will cause termination (because the protocol
  775.   ;; doesn't support chaining).  [Alternatively, font changes could be accepted
  776.   ;; as long as they are accompanied with a width return value, or always
  777.   ;; accept font changes and call text-width as required.  However, horizontal
  778.   ;; motion can't really be accepted, due to semantics.]  First result is new
  779.   ;; start, if end was not reached.  Second result is overall width, if known.
  780.   (declare (type drawable drawable)
  781.        (type gcontext gcontext)
  782.        (type int16 x y)
  783.        (type array-index start)
  784.        (type (or null array-index) end)
  785.        (type sequence sequence)
  786.        (type (or null int32) width)
  787.        (type (or null translation-function) translate) 
  788.        (type index-size size))
  789.   (declare-values (or null array-index) (or null int32))
  790.   (unless end (setq end (length sequence)))
  791.   (ecase size
  792.     ((:default 8)
  793.      (draw-image-glyphs8 drawable gcontext x y sequence start end width translate))
  794.     (16
  795.      (draw-image-glyphs16 drawable gcontext x y sequence start end width translate))))
  796.  
  797. (defun draw-image-glyphs8 (drawable gcontext x y sequence start end width translate)
  798.   ;; An initial font change is allowed from translate, but any subsequent font
  799.   ;; change or horizontal motion will cause termination (because the protocol
  800.   ;; doesn't support chaining).  [Alternatively, font changes could be accepted
  801.   ;; as long as they are accompanied with a width return value, or always
  802.   ;; accept font changes and call text-width as required.  However, horizontal
  803.   ;; motion can't really be accepted, due to semantics.]  First result is new
  804.   ;; start, if end was not reached.  Second result is overall width, if known.
  805.   (declare (type drawable drawable)
  806.        (type gcontext gcontext)
  807.        (type int16 x y)
  808.        (type array-index start)
  809.        (type sequence sequence)
  810.        (type (or null array-index) end)
  811.        (type (or null int32) width)
  812.        (type (or null translation-function) translate)) 
  813.   (declare-values (or null array-index) (or null int32))
  814.   (do* ((display (gcontext-display gcontext))
  815.     (length (index- end start))
  816.     ;; Should metrics-p be T?  Don't want to pass a NIL font into translate...
  817.     (font (gcontext-font gcontext t))
  818.     (font-change nil)
  819.     (new-start) (translated-width) (chunk))
  820.        (nil) ;; forever
  821.     (declare (type display display)
  822.          (type array-index length)
  823.          (type (or null array-index) new-start chunk))
  824.     
  825.     (when font-change
  826.       (setf (gcontext-font gcontext) font))
  827.     (block change-font
  828.       (with-buffer-request (display *x-imagetext8* :gc-force gcontext :length length)
  829.     (drawable drawable)
  830.     (gcontext gcontext)
  831.     (int16 x y)
  832.     (progn
  833.       ;; Translate the sequence into the buffer
  834.       (multiple-value-setq (new-start font translated-width)
  835.         (funcall (or translate #'translate-default) sequence start end
  836.              font buffer-bbuf (index+ buffer-boffset 16)))
  837.       ;; Number of glyphs translated
  838.       (setq chunk (index- new-start start))        
  839.       ;; Check for initial font change
  840.       (when (and (index-zerop chunk) (type? font 'font))
  841.         (setq font-change t) ;; Loop around changing font
  842.         (return-from change-font))
  843.       ;; Quit when nothing translated
  844.       (when (index-zerop chunk)
  845.         (return-from draw-image-glyphs8 new-start))
  846.       ;; Update buffer pointers
  847.       (data-put 1 chunk)
  848.       (let ((blen (lround (index+ 16 chunk))))
  849.         (length-put 2 (index-ash blen -2))
  850.         (setf (buffer-boffset display) (index+ buffer-boffset blen)))))
  851.       ;; Normal exit
  852.       (return-from draw-image-glyphs8
  853.     (values (if (index= chunk length) nil new-start)
  854.         (or translated-width width))))))
  855.  
  856. (defun draw-image-glyphs16 (drawable gcontext x y sequence start end width translate)
  857.   ;; An initial font change is allowed from translate, but any subsequent font
  858.   ;; change or horizontal motion will cause termination (because the protocol
  859.   ;; doesn't support chaining).  [Alternatively, font changes could be accepted
  860.   ;; as long as they are accompanied with a width return value, or always
  861.   ;; accept font changes and call text-width as required.  However, horizontal
  862.   ;; motion can't really be accepted, due to semantics.]  First result is new
  863.   ;; start, if end was not reached.  Second result is overall width, if known.
  864.   (declare (type drawable drawable)
  865.        (type gcontext gcontext)
  866.        (type int16 x y)
  867.        (type array-index start)
  868.        (type sequence sequence)
  869.        (type (or null array-index) end)
  870.        (type (or null int32) width)
  871.        (type (or null translation-function) translate))
  872.   (declare-values (or null array-index) (or null int32))
  873.   (do* ((display (gcontext-display gcontext))
  874.     (length (index- end start))
  875.     ;; Should metrics-p be T?  Don't want to pass a NIL font into translate...
  876.     (font (gcontext-font gcontext t)) 
  877.     (font-change nil)
  878.     (new-start) (translated-width) (chunk)
  879.     (buffer (buffer-tbuf16 display)))
  880.        (nil) ;; forever
  881.     
  882.     (declare (type display display)
  883.          (type array-index length)
  884.          (type (or null array-index) new-start chunk)
  885.          (type buffer-text16 buffer))
  886.     (when font-change
  887.       (setf (gcontext-font gcontext) font))
  888.  
  889.     (block change-font
  890.       (with-buffer-request (display *x-imagetext16* :gc-force gcontext :length length)
  891.     (drawable drawable)
  892.     (gcontext gcontext)
  893.     (int16 x y)
  894.     (progn
  895.       ;; Translate the sequence into the buffer
  896.       (multiple-value-setq (new-start font translated-width)
  897.         (funcall (or translate #'translate-default) sequence start end
  898.              font buffer 0))
  899.       ;; Number of glyphs translated
  900.       (setq chunk (index- new-start start))
  901.       ;; Check for initial font change
  902.       (when (and (index-zerop chunk) (type? font 'font))
  903.         (setq font-change t) ;; Loop around changing font
  904.         (return-from change-font))
  905.       ;; Quit when nothing translated
  906.       (when (index-zerop chunk)
  907.         (return-from draw-image-glyphs16 new-start))
  908.       (write-sequence-card16 display (index+ buffer-boffset 16) buffer 0 chunk
  909.                  #+clx-little-endian ;; Byte swap for little-endian
  910.                  #'byte-swap-card16)
  911.       ;; Update buffer pointers
  912.       (data-put 1 chunk)
  913.       (let ((blen (lround (index+ 16 (index-ash chunk 1)))))
  914.         (length-put 2 (index-ash blen -2))
  915.         (setf (buffer-boffset display) (index+ buffer-boffset blen)))))
  916.       ;; Normal exit
  917.       (return-from draw-image-glyphs16
  918.     (values (if (index= chunk length) nil new-start)
  919.         (or translated-width width))))))
  920.  
  921.  
  922. ;;-----------------------------------------------------------------------------
  923.  
  924. (defun display-keycode-range (display)
  925.   (declare (type display display))
  926.   (declare-values min max)
  927.   (values (display-min-keycode display)
  928.       (display-max-keycode display)))
  929.  
  930. ;; Should this signal device-busy like the pointer-mapping setf, and return a
  931. ;; boolean instead (true for success)?  Alternatively, should the
  932. ;; pointer-mapping setf be changed to set-pointer-mapping with a (member
  933. ;; :success :busy) result?
  934.  
  935. (defun set-modifier-mapping (display &key shift lock control mod1 mod2 mod3 mod4 mod5)
  936.   ;; Setf ought to allow multiple values.
  937.   (declare (type display display)
  938.        (type sequence shift lock control mod1 mod2 mod3 mod4 mod5))
  939.   (declare-values (member :success :busy :failed))
  940.   (let* ((keycodes-per-modifier (index-max (length shift)
  941.                        (length lock)
  942.                        (length control)
  943.                        (length mod1)
  944.                        (length mod2)
  945.                        (length mod3)
  946.                        (length mod4)
  947.                        (length mod5)))
  948.      (data (make-array (index* 8 keycodes-per-modifier)
  949.                :element-type 'card8
  950.                :initial-element 0))
  951.      result)
  952.     (replace data shift)
  953.     (replace data lock :start1 keycodes-per-modifier)
  954.     (replace data control :start1 (index* 2 keycodes-per-modifier))
  955.     (replace data mod1 :start1 (index* 3 keycodes-per-modifier))
  956.     (replace data mod2 :start1 (index* 4 keycodes-per-modifier))
  957.     (replace data mod3 :start1 (index* 5 keycodes-per-modifier))
  958.     (replace data mod4 :start1 (index* 6 keycodes-per-modifier))
  959.     (replace data mod5 :start1 (index* 7 keycodes-per-modifier))
  960.     (with-display (display)
  961.       (with-buffer-request (display *x-setmodifiermapping* :no-after)
  962.     (data keycodes-per-modifier)
  963.     ((sequence :format card8) data))
  964.       (with-buffer-reply (display 4 :sizes 8)
  965.     (setq result (member8-get 1 :success :busy :failed))))
  966.     (display-invoke-after-function display)
  967.     result))
  968.  
  969. #+comment ; beta protocol
  970. (defun set-modifier-mapping (display &key shift lock control mod1 mod2 mod3 mod4 mod5)
  971.   ;; Setf ought to allow multiple values.
  972.   (declare (type display display)
  973.        (type (or null integer) lock)
  974.        ;; (list integer) is really a list of length 2
  975.        (type (or null card8 list) shift control
  976.          mod1 mod2 mod3 mod4 mod5))
  977.   (declare-values (member :success :busy :failed))
  978.   (let (result)
  979.     (with-display (display)
  980.       (macrolet ((a (value) `(if (consp ,value) (car ,value) (or ,value 0)))
  981.          (b (value) `(if (consp ,value) (cadr ,value) 0)))
  982.     (with-buffer-request (display *x-setmodifiermapping* :no-after)
  983.       (data (or lock 0))
  984.       (card8
  985.        (a shift) (b shift)
  986.        (a control) (b control)
  987.        (a mod1) (b mod1)
  988.        (a mod2) (b mod2)
  989.        (a mod3) (b mod3)
  990.        (a mod4) (b mod4)
  991.        (a mod5) (b mod5))))
  992.       (with-buffer-reply (display 4 :sizes 8)
  993.     (setq result (member8-get 1 :success :busy :failed))))
  994.     (display-invoke-after-function display)
  995.     result))
  996.  
  997. (defun modifier-mapping (display)
  998.   ;; each value is a list of integers
  999.   (declare (type display display))
  1000.   (declare-values shift lock control mod1 mod2 mod3 mod4 mod5)
  1001.   (let ((lists nil))
  1002.     (with-display (display)
  1003.       (with-buffer-request (display *x-getmodifiermapping* :no-after)
  1004.     )
  1005.       (with-buffer-reply (display nil :sizes 8)
  1006.     (do* ((keycodes-per-modifier (card8-get 1))
  1007.           (keys nil nil)
  1008.           (i 0 (index+ i 1)))
  1009.          ((index= i 8))
  1010.       (buffer-input display buffer-bbuf 0 keycodes-per-modifier)
  1011.       (dotimes (j keycodes-per-modifier)
  1012.         (let ((key (aref-card8 buffer-bbuf j)))
  1013.           (unless (zerop key)
  1014.         (push key keys))))
  1015.       (push (nreverse keys) lists))))
  1016.     (display-invoke-after-function display)
  1017.     (values-list (nreverse lists))))
  1018.  
  1019. #+comment ; beta protocol
  1020. (defun modifier-mapping (display)
  1021.   ;; each value is either nil, a single integer, or a list of two integers
  1022.   (declare (type display display))
  1023.   (declare-values shift lock control mod1 mod2 mod3 mod4 mod5)
  1024.   (let (shift lock control mod1 mod2 mod3 mod4 mod5)
  1025.     (with-display (display)
  1026.       (with-buffer-request (display *x-getmodifiermapping* :no-after)
  1027.     )
  1028.       (with-buffer-reply (display 22 :sizes 8)
  1029.     (macrolet ((combine (a b)
  1030.                 (let ((the-a (gensym))
  1031.                   (the-b (gensym)))
  1032.                   `(let ((,the-a ,a)
  1033.                      (,the-b ,b))
  1034.                  (if (plusp ,the-b) (list ,the-a ,the-b)
  1035.                    (and (plusp ,the-a) ,the-a))))))
  1036.       (setq lock (card8-get 1)
  1037.         shift (combine (card8-get 8) (card8-get 9))
  1038.         control (combine (card8-get 10) (card8-get 11))
  1039.         mod1 (combine (card8-get 12) (card8-get 13))
  1040.         mod2 (combine (card8-get 14) (card8-get 15))
  1041.         mod3 (combine (card8-get 16) (card8-get 17))
  1042.         mod4 (combine (card8-get 18) (card8-get 19))
  1043.         mod5 (combine (card8-get 20) (card8-get 21))))))
  1044.     (display-invoke-after-function display)
  1045.     (values shift lock control mod1 mod2 mod3 mod4 mod5)))
  1046.  
  1047. ;; Either we will want lots of defconstants for well-known values, or perhaps
  1048. ;; an integer-to-keyword translation function for well-known values.
  1049.  
  1050. (defun change-keyboard-mapping
  1051.        (display keysyms &key (start 0) end (first-keycode start))
  1052.   ;; start/end give subrange of keysyms
  1053.   ;; first-keycode is the first-keycode to store at
  1054.   (declare (type display display)
  1055.        (type array-index start)
  1056.        (type card8 first-keycode)
  1057.        (type (or null array-index) end)
  1058.        (type (array * (* *))))
  1059.   (let* ((keycode-end (or end (array-dimension keysyms 0)))
  1060.      (keysyms-per-keycode (array-dimension keysyms 1))
  1061.      (length (index- keycode-end start))
  1062.      (size (* length keysyms-per-keycode)))
  1063.     (declare (type array-index keycode-end keysyms-per-keycode length))
  1064.     (with-buffer-request (display *x-setkeyboardmapping* :sizes (32))
  1065.       (data length)
  1066.       (length (+ size 2))
  1067.       (card8 first-keycode keysyms-per-keycode)
  1068.       (progn
  1069.     (do ((limit (index-ash (buffer-limit display) -2))
  1070.          (w (index+ 2 (index-ash buffer-boffset -2)))
  1071.          (i start (index+ i 1)))
  1072.         ((index>= i keycode-end)
  1073.          (setf (buffer-boffset display) (index-ash w 2)))
  1074.       (declare (type array-index limit w i))
  1075.       (when (index> w limit)
  1076.         (buffer-flush display)
  1077.         (setq w (index-ash (buffer-boffset display) -2)))
  1078.       (do ((j 0 (index+ j 1)))
  1079.           ((index>= j keysyms-per-keycode))
  1080.         (declare (type array-index j))
  1081.         (card29-put (index* w 4) (aref keysyms i j))
  1082.         (index-incf w)))))))
  1083.  
  1084. (defun keyboard-mapping (display &key first-keycode start end data)
  1085.   ;; First-keycode specifies which keycode to start at (defaults to min-keycode).
  1086.   ;; Start specifies where (in result) to put first-keycode. (defaults to first-keycode)
  1087.   ;; (- end start) is the number of keycodes to get. (End defaults to (1+ max-keycode)).
  1088.   ;; If DATA is specified, the results are put there.
  1089.   (declare (type display display)
  1090.        (type (or null card8) first-keycode)
  1091.        (type (or null array-index) start end)
  1092.        (type (or null (array * (* *))) data))
  1093.   (declare-values (array * (* *)))
  1094.   (unless first-keycode (setq first-keycode (display-min-keycode display)))
  1095.   (unless start (setq start first-keycode))
  1096.   (unless end (setq end (1+ (display-max-keycode display))))
  1097.   (with-display (display)
  1098.     (with-buffer-request (display *x-getkeyboardmapping* :no-after)
  1099.       (card8 first-keycode (- end start)))
  1100.     (with-buffer-reply (display nil :sizes (8 32))
  1101.       (do* ((keysyms-per-keycode (card8-get 1))
  1102.         (bytes-per-keycode (* keysyms-per-keycode 4))
  1103.         (keycode-count (floor (card32-get 4) keysyms-per-keycode)
  1104.                (1- keycode-count))
  1105.         (result (if (and (arrayp data)
  1106.                  (= (array-rank data) 2)
  1107.                  (>= (array-dimension data 0) (+ start keycode-count))
  1108.                  (>= (array-dimension data 1) keysyms-per-keycode))
  1109.             data
  1110.               (make-array `(,(+ start keycode-count) ,keysyms-per-keycode)
  1111.                   :element-type 'keysym :initial-element 0)))
  1112.         (i start (1+ i)))
  1113.        ((zerop keycode-count) (setq data result))
  1114.     (buffer-input display buffer-bbuf 0 bytes-per-keycode)
  1115.     (dotimes (j keysyms-per-keycode)
  1116.       (setf (aref result i j) (card29-get (* j 4)))))))
  1117.   (display-invoke-after-function display)
  1118.   data)
  1119.